package com.example.administrator.androidui3;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.List;

public class CoursesActivity extends AppCompatActivity {
    List<Course> courseList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_courses);

        //将要数据呈现到listview中
        initData();
        initView();
    }

    private void initView() {
        //获取listview控件
        ListView lvCorses = findViewById(R.id.lv_courses);
        // 创建适配器对象
        CourseAdapter adapter = new CourseAdapter();
        //将适配器绑定到控件
        lvCorses.setAdapter(adapter);
    }

    private void initData() {
        Gson gson = new Gson();
        Type listType = new TypeToken<List<Course>>() {
        }.getType();
        InputStream in = getResources().openRawResource(R.raw.data);
        Reader reader = new InputStreamReader(in);
        courseList = gson.fromJson(reader, listType);
    }

    class CourseAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return courseList.size();// 获取要显示的数据项数目
        }

        @Override
        public Object getItem(int position) {
            return courseList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //导入布局到试图
            View view = View.inflate(CoursesActivity.this, R.layout.list_item, null);
            //从list_item中获取图片空间,并设置控件显示的图片
            ImageView imageView = view.findViewById(R.id.iv_course);
            final Course course = courseList.get(position);
            if (course.getCourseImage().equals("cc.png")) {
                imageView.setImageResource(R.drawable.cc);
            } else if (course.getCourseImage().equals("java.png")) {
                imageView.setImageResource(R.drawable.java);
            } else if (course.getCourseImage().equals("os.png")) {
                imageView.setImageResource(R.drawable.os);
            }


            TextView tvCourseName = view.findViewById(R.id.tv_course_name);
            TextView tvCourseSchool = view.findViewById(R.id.tv_school_name);
            TextView tvCourseCount = view.findViewById(R.id.tv_person_count);
            TextView tvCourseType = view.findViewById(R.id.tv_course_type);
            tvCourseName.setText(course.getCourseName());
            tvCourseSchool.setText(course.getCourseSchool());
            tvCourseCount.setText(course.getCourseCount()+"");
            tvCourseType.setText(course.getCourseType());
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(CoursesActivity.this, course.getCourseName(), Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(CoursesActivity.this, MainActivity.class);
                    intent.putExtra("coursename", course.getCourseName());
                    startActivity(intent);
                    CoursesActivity.this.finish();
                }
            });
            return view;
        }
    }
}
